import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * Created by fjc on 2017/3/14.
 */
public class SortedSet {
    //直接创建jedis
//    Jedis jedis = new Jedis("localhost",6379);
    //创建对象池
    JedisPool jedisPool = new JedisPool(new JedisPoolConfig(),"localhost",6379);
    //获取jedis
    Jedis jedis = jedisPool.getResource();

    @Test
    public void zAdd(){
        //向有序集合添加一个或多个成员，或者更新已存在成员
        jedis.zadd("z1",90,"王尼玛");
        jedis.zadd("z1",91,"王尼美");
        jedis.zadd("z1",92,"木子");
        jedis.zadd("z1",94,"张全蛋");
    }

    @Test
    public void zRange(){
        //通过索引区间返回有序集合成指定区间内的成员
        System.out.println(jedis.zrange("z1",0,-1));
    }

    @Test
    public void zScore(){
        // 返回有序集中，成员的分数值
        System.out.println(jedis.zscore("z1","木子"));
    }

    @Test
    public void zCard(){
        //获取有序集合的成员数
        System.out.println(jedis.zcard("z1"));
    }

    @Test
    public void zCount(){
        //计算在有序集合中指定区间分数的成员数
        System.out.println(jedis.zcount("z1",90,93));
    }

    @Test
    public void zIncrBy(){
        //有序集合中对指定成员的分数加上增量
        jedis.zincrby("z1",3,"木子");
        System.out.println(jedis.zscore("z1","木子"));
    }

    @Test
    public void zInterStore(){
        //计算给定的一个或多个有序集的交集并将结果集存储在新的有序集合 key 中
        jedis.zadd("z1",90,"王尼玛");
        jedis.zadd("z1",91,"王尼美");
        jedis.zadd("z1",92,"木子");
        jedis.zadd("z1",94,"张全蛋");
        jedis.zadd("z2",90,"铁柱");
        jedis.zadd("z2",91,"纸巾老师");
        jedis.zadd("z2",92,"最帅旁白");
        jedis.zadd("z2",94,"大队长");
        jedis.zinterstore("z3","z1","z2");
        System.out.println(jedis.zcount("z1",90,93));
    }

    @Test
    public void zlexCount(){
        //在有序集合中计算指定字典区间内成员数量
        System.out.println(jedis.zlexcount("z1","-","+"));
    }

    @Test
    public void zRangeByLex(){
        //通过字典区间返回有序集合的成员
        System.out.println(jedis.zrangeByLex("z1","-","+"));
    }

    @Test
    public void zRank(){
        //返回有序集合中指定成员的索引
        System.out.println(jedis.zrank("z1","木子"));
    }

    @Test
    public void zRem(){
        //移除有序集合中的一个或多个成员
        jedis.zrem("z1","王尼美");
        System.out.println(jedis.zrange("z1",0,-1));
    }

    @Test
    public void zRemRangeByLex(){
        //移除有序集合中给定的字典区间的所有成员
        jedis.zremrangeByLex("z1","-","+");
        System.out.println(jedis.zrange("z1",0,-1));
    }

    @Test
    public void zRemRangeByRank(){
        //移除有序集合中给定的排名区间的所有成员
        System.out.println(jedis.zremrangeByRank("z1",0,-1));
    }

    @Test
    public void zRemRangeByScore(){
        //移除有序集合中给定的分数区间的所有成员
        jedis.zremrangeByScore("z1",90,93);
        System.out.println(jedis.zrange("z1",0,-1));
    }

    @Test
    public void zRevRange(){
        //返回有序集中指定区间内的成员，通过索引，分数从高到底
        System.out.println(jedis.zrevrange("z1",90,93));
    }

    @Test
    public void zRevRangeByScore(){
        //返回有序集中指定分数区间内的成员，分数从高到低排序
        System.out.println(jedis.zrevrangeByScore("z1",90,93));
    }

    @Test
    private void zRevRank(){
        //返回有序集合中指定成员的排名，有序集成员按分数值递减(从大到小)排序
        System.out.println(jedis.zrevrank("z1","木子"));
    }

    @Test
    private void zUnionStore(){
        //计算给定的一个或多个有序集的并集，并存储在新的 key 中
        jedis.zadd("z1",90,"王尼玛");
        jedis.zadd("z1",91,"王尼美");
        jedis.zadd("z1",92,"木子");
        jedis.zadd("z1",94,"张全蛋");
        jedis.zadd("z2",90,"铁柱");
        jedis.zadd("z2",91,"纸巾老师");
        jedis.zadd("z2",92,"最帅旁白");
        jedis.zadd("z2",94,"大队长");
        jedis.zunionstore("z3","z1","z2");
        System.out.println(jedis.zrange("z3",0,-1));
    }

}
